package com.vf.cloud.paas.app.master.api.product.controller;

import javax.servlet.http.HttpServletRequest;
import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.vf.cloud.paas.app.master.api.file.service.IThumbnail;
import com.vf.cloud.paas.app.master.common.domain.Product;
import com.vf.cloud.paas.app.master.common.domain.ProductFun;
import com.vf.cloud.paas.app.master.common.repository.base.Thumbnail;
import com.vf.cloud.paas.common.datasource.annotation.Tx;
import com.vf.cloud.paas.common.log.annotation.RLog;
import com.vf.cloud.paas.common.security.annotation.TokenRequired;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/product")
public class ProductController {

	@Autowired
	private IThumbnail thumbnailImpl;

	
	@RLog(value = "分页获取组件列表", type = "user")
	@TokenRequired
	@GetMapping(value = { "/pages" }, produces = "application/json; charset=utf-8")
	public R<Page<Product>> pages(HttpServletRequest request,
			@RequestParam(name = "classifyId", required = false, defaultValue = "") String classifyId,
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) throws Exception {
		String sqlExceptSelect = " from " + Product.TABLE_NAME + "  a WHERE a.classify_id='" +classifyId
				+ "' ";
		sqlExceptSelect += "  ORDER BY a.name  , a.modify_time desc ";
		if (!StrKit.isBlank(keywords)) {
			sqlExceptSelect += " and name like '%" + keywords + "%' ";
		}
		return R.ok(Product.dao.paginate(cur, limit, "select * ", sqlExceptSelect));
	}

	@RLog(value = "保存组件", type = "user")
	@TokenRequired
	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<Product> save(MultipartFile thumbnailFile, Product product) {
		try {
			Thumbnail thumbnailEntity = null;
			if (thumbnailFile != null) {
				thumbnailEntity = new Thumbnail();
				thumbnailEntity.setSize(thumbnailFile.getSize());
				thumbnailEntity.setName(product.getName());
				thumbnailEntity.setContent(new Binary(thumbnailFile.getBytes()));
			}

			if (StrKit.notBlank(product.getId())) {
				if (product.update()) {
					if (thumbnailEntity != null) {
						try {
							thumbnailEntity.setId(product.getId());
							thumbnailImpl.save(thumbnailEntity);
						} catch (Exception e) {
						}
					}
					return R.ok(product);
				} else {
					return R.failed("更新失败！");
				}
			} else {
				product.setId(UuidUtil.getUUID());
				if (product.save()) {
					if (thumbnailEntity != null) {
						try {
							thumbnailEntity.setId(product.getId());
							thumbnailImpl.save(thumbnailEntity);
						} catch (Exception e) {
						}
					}
					return R.ok(product);
				} else {
					return R.failed("保存失败！");
				}
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@RLog(value = "删除组件",type = "user")
	@TokenRequired
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public R<String> delete(@RequestBody Product product) {
		if (product.delete()) {
			thumbnailImpl.deleteById(product.getId());
			Db.delete("delete from " + ProductFun.TABLE_NAME + " where product_id=?", product.getId());
			return R.ok();
		}
		return R.failed();
	}

}
